package com.assignwork.service.impl;

import com.assignwork.dto.CorpMachineSearchDTO;
import com.assignwork.dto.CorpMachineSubmitDTO;
import com.assignwork.entity.CorpMachine;
import com.assignwork.mapper.CorpMachineMapper;
import com.assignwork.service.ICorpMachineService;
import com.assignwork.vo.CorpMachineListVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.common.cache.CacheNames;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.system.cache.DictBizCache;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @ClassName CorpMachineServiceImpl
 * @Description 单位机械接口实现
 * @Author dujianyu
 * @Date 2023/12/8 9:51
 */
@Service
public class CorpMachineServiceImpl extends BaseServiceImpl<CorpMachineMapper, CorpMachine> implements ICorpMachineService {
    @Override
    public IPage<CorpMachineListVO> selectCorpMachinePage(CorpMachineSearchDTO dto, IPage<CorpMachineListVO> page) {
        List<CorpMachineListVO> data = baseMapper.selectCorpMachineList(dto, page);
        return page.setRecords(dataProcess(data));
    }

    @Override
    public List<CorpMachineListVO> selectCorpMachineList(CorpMachineSearchDTO dto) {
        List<CorpMachineListVO> data = baseMapper.selectCorpMachineList(dto, null);
        return dataProcess(data);
    }

    @Override
    public boolean submitCorpMachine(CorpMachineSubmitDTO dto) {
        CorpMachine entity = BeanUtil.copy(dto, CorpMachine.class);
        return super.saveOrUpdate(entity);
    }

    @Override
    public List<CorpMachineListVO> listFixedMachine(CorpMachineSearchDTO dto) {
        return dataProcess(baseMapper.selectCorpMachineList(dto, null));
    }

    private List<CorpMachineListVO> dataProcess(List<CorpMachineListVO> data) {
        for (CorpMachineListVO item : data) {
            item.setMachineKindName(DictBizCache.getValue("machine_kind", item.getMachineKind()));
            item.setMachineCategoryName(DictBizCache.getValue(CacheNames.DICT_MACHINE_CATEGORY, item.getMachineCategory()));
            item.setMachinePositionName(DictBizCache.getValue(CacheNames.DICT_MACHINE_POSITION, item.getMachinePosition()));
            item.setMachineTypeName(DictBizCache.getValue(CacheNames.DICT_MACHINE_TYPE, item.getMachineTypeNo()));
        }
        return data;
    }
}
